package com.ydl.shopping.security.handle;

import cn.hutool.json.JSONUtil;
import com.ydl.shopping.core.enums.ResultEnum;
import com.ydl.shopping.core.vo.ResultVO;
import com.ydl.shopping.utils.web.WebUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 授权认证错误处理器
 *
 * @author 林武泰
 */
@Component
@Slf4j
public class AccessDeniedHandleImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        ResultVO resultVo = ResultVO.resultEnum(ResultEnum.FORBIDDEN);
        String result = JSONUtil.toJsonStr(resultVo);
        WebUtil.renderString(response, result);
    }
}
